package bdd;

import java.sql.*;


public class Traitement {
	
	private Connect connection = new Connect();

	public Traitement(){
		connection.connection();
	}
	
	public void ajouterScenario(String nomScenario, int tailleGrille, int nbPas, int vitesse, String arbres[][]) throws SQLException{
		ResultSet MesResultats;
		int id = 0;
		int type = 0;
		int posX = 0;
		int posY = 0;
		
		java.sql.Connection conn = connection.getConn();
		String ajoutParam="INSERT INTO parametre(nom,tailleGrille,nbPas,vitesse) VALUES('"+nomScenario+"',"+(tailleGrille-2)+","+nbPas+","+vitesse+");";
		Statement stmt = conn.createStatement();
		int ligne = stmt.executeUpdate(ajoutParam);
		
		String selectScenario="SELECT MAX(id) FROM parametre;";
		Statement stmt2 = conn.createStatement();
		MesResultats = stmt2.executeQuery(selectScenario);
        while (MesResultats.next()) {
            id = MesResultats.getInt(1);
        }
		
		for (int i = 0; i < tailleGrille; i++) {
			for (int j = 0; j < tailleGrille; j++) {
				
				posX=i;
				posY=j;
				switch(arbres[i][j]){
				case "jeune_pousse":
					type=1;
					break;
				case "arbuste":
					type=2;
					break;
				case "arbuste2":
					type=2;
					break;
				case "arbre":
					type=3;
					break;
				case "feu":
					type=4;
					break;
				case "insecte":
					type=5;
					break;
				case "vide":
					type=0;
					break;
				}
				String ajoutCellule="INSERT INTO cellule(idParam,type,posX,posY) VALUES('"+id+"',"+type+","+posX+","+posY+");";
				Statement stmt3 = conn.createStatement();
				int ligne2 = stmt.executeUpdate(ajoutCellule); 
			}
		}
		
	}


	public String[] remplirComboBox() throws SQLException{
		int i=0;
		int j=0;

		java.sql.Connection conn = connection.getConn();
		
		String req= "SELECT COUNT(nom) FROM parametre;";
		Statement stmt = conn.createStatement();
		ResultSet res = stmt.executeQuery(req);
		while(res.next()){
				i=res.getInt(1);
		}
		
		String scenario[] = new String[i];
		String req2= "SELECT nom FROM parametre ORDER BY nom;";
		Statement stmt2 = conn.createStatement();
		ResultSet res2 = stmt2.executeQuery(req2);
		while(res2.next()){
			scenario[j]=res2.getString("nom");
			j++;

		}
		return scenario;
		
	}
	
	public String[] chargerScenario(String nomScenario) throws SQLException{
		java.sql.Connection conn = connection.getConn();
		String parametre[] = new String[3];
		
		String selectScenario="SELECT tailleGrille, nbPas, vitesse FROM parametre WHERE nom ='"+ nomScenario +"';";
		Statement stmt = conn.createStatement();
		ResultSet Res = stmt.executeQuery(selectScenario);
		while(Res.next()){
			parametre[0]=Res.getString("tailleGrille");
			parametre[1]=Res.getString("nbPas");
			parametre[2]=Res.getString("vitesse");

		}
		return parametre;
	}
	
	public String[][] chargerCellule(String nomScenario) throws SQLException{
		int tailleGrille = 0;
		int idParam = 0;
		int whoseType = 0;
		int whoseX = 0;
		int whoseY = 0;
		java.sql.Connection conn = connection.getConn();
		
		String selectScenario="SELECT tailleGrille,id FROM parametre WHERE nom ='"+ nomScenario +"';";
		Statement stmt = conn.createStatement();
		ResultSet Res = stmt.executeQuery(selectScenario);
		while(Res.next()){
			tailleGrille=Res.getInt("tailleGrille")+2;
			idParam=Res.getInt("id");
		}
		
		
		String arbres[][] = new String[tailleGrille][tailleGrille];
		String selectCellule="SELECT type, posX, posY FROM cellule WHERE idParam='"+ idParam +"';";
		Statement stmt2 = conn.createStatement();
		ResultSet Res2 = stmt2.executeQuery(selectCellule);
		while(Res2.next()){
				whoseType=Res2.getInt("type");
				whoseX=Res2.getInt("posX");
				whoseY=Res2.getInt("posY");
					switch(whoseType){
					case 1:
						arbres[whoseX][whoseY]="jeune_pousse";
						break;
					case 2:
						arbres[whoseX][whoseY]="arbuste";
						break;
					case 3:
						arbres[whoseX][whoseY]="arbre";
						break;
					case 4:
						arbres[whoseX][whoseY]="feu";
						break;
					case 5:
						arbres[whoseX][whoseY]="insecte";
						break;
					case 0:
						arbres[whoseX][whoseY]="vide";
						break;
					}

		}
		return arbres;
	}
}